﻿#include "mymainwindow.h"
#include<QCoreApplication>
//1+ enemy  2+ friend
MyMainWindow::MyMainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    //----------initalize
    init();

    //----------put pictureLayer
    layer=new PictureLayer(this,Qt::AlignTop,Qt::AlignRight,QPoint(0,0));
    //layer->setFixedSize(this->size());
    layer->mouseFilter=new MouseFilter(layer);
    layer->installEventFilter(layer->mouseFilter);
    connect(layer->mouseFilter, &MouseFilter::mouseEventReceived, [=](QEvent *event) {
        // 在这里处理鼠标事件，可以根据需要进行相应的处理
        QCoreApplication::sendEvent(this->webView, event);
    });

    //----------Stackwindow load
    leftDockWindow=new LeftDockWindow(this,Qt::AlignTop,Qt::AlignLeft,QPoint(0,leftDockWindowInitY));
    connect(leftDockWindow->mouseFilter, &MouseFilter::mouseEventReceived, [=](QEvent *event) {
        // 在这里处理鼠标事件，可以根据需要进行相应的处理
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        // 在这里修改鼠标位置，例如将 y 方向减去 100
        QMouseEvent* newMouseEvent = new QMouseEvent(
                    mouseEvent->type(),
                    QPointF(mouseEvent->pos().x(), mouseEvent->pos().y() + leftDockWindowInitY),
                    mouseEvent->screenPos(),
                    mouseEvent->button(),
                    mouseEvent->buttons(),
                    mouseEvent->modifiers()
                    );
        QCoreApplication::sendEvent(this->webView, event);
        QCoreApplication::sendEvent(layer,newMouseEvent);
    });
    connect(leftDockWindow->plottingButton,&TargetPlottingButton::clicked,[=](){
        if(layer->paintingStatus==0)
            layer->paintingStatus=1;
        else if(layer->paintingStatus==1||layer->paintingStatus){
            layer->paintingStatus=0;
        }

    });
    connect(leftDockWindow->identiButton,&IdentificationButton::clicked,[=](){
        layer->helicopter->isMarked=false;
        for(int i=0;i<layer->elementVector.size();i++){
            layer->elementVector[i]->isMarked=false;
            layer->elementVector[i]->isExist=false;
            layer->DroneResponse=false;
        }
        layer->helicopter->markerVector[0]->isExist=true;
        layer->update();
    });

    rightDockWindow=new RightDockWindow(this,Qt::AlignTop,Qt::AlignRight,QPoint(this->width()-25,rightDockWindowInitX));
    connect(rightDockWindow->mouseFilter, &MouseFilter::mouseEventReceived, [=](QEvent *event) {
        // 在这里处理鼠标事件，可以根据需要进行相应的处理
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        // 在这里修改鼠标位置，例如将 y 方向减去 100
        QMouseEvent* newMouseEvent = new QMouseEvent(
                    mouseEvent->type(),
                    QPointF(mouseEvent->pos().x()+rightDockWindowInitX, mouseEvent->pos().y() + rightDockWindowInitY),
                    mouseEvent->screenPos(),
                    mouseEvent->button(),
                    mouseEvent->buttons(),
                    mouseEvent->modifiers()
                    );
        QCoreApplication::sendEvent(this->webView, event);
        QCoreApplication::sendEvent(layer,newMouseEvent);
    });

    topRightDockWindow=new TopRightDockWindow(this,Qt::AlignTop,Qt::AlignLeft,QPoint(this->width()-110,-25));
    topLeftDockWindow=new TopLeftDockWindow(this,Qt::AlignTop,Qt::AlignRight,QPoint(0,0));
    bottomDockWindow=new BottomDockWindow(this,Qt::AlignTop,Qt::AlignRight,QPoint(0,this->height()-100));

    //----------insert stackWindow
    stackMap.insert("leftDockWindow",leftDockWindow);
    stackMap.insert("rightDockWindow",rightDockWindow);
    stackMap.insert("topRightDockWindow",topRightDockWindow);
    stackMap.insert("topLeftDockWindow",topLeftDockWindow);
    stackMap.insert("bottomDockWindow",bottomDockWindow);

    //PowerKey
    connect(topRightDockWindow,&TopRightDockWindow::closeWindow,[=](){
        this->creatQuitMessageBox();
    });

    //paint
    connect(leftDockWindow->plottingButton->enemyPlanebtn,&QPushButton::clicked,[=](){
        if(layer->paintingStatus!=11)
            layer->paintingStatus=11;
    });
    connect(leftDockWindow->plottingButton->enemyTankbtn,&QPushButton::clicked,[=](){
        if(layer->paintingStatus!=12)
            layer->paintingStatus=12;
    });
    connect(leftDockWindow->plottingButton->enemyBunkerbtn,&QPushButton::clicked,[=](){
        if(layer->paintingStatus!=13)
            layer->paintingStatus=13;
    });
    connect(leftDockWindow->plottingButton->friendUAVbtn,&QPushButton::clicked,[=](){
        if(layer->paintingStatus!=21)
            layer->paintingStatus=21;
    });
    connect(leftDockWindow->missionButton,&QPushButton::clicked,[=](){
        layer->paintingStatus=0;
    });
    connect(leftDockWindow->missionButton->routeMarker,&QPushButton::clicked,[=](){
        if(layer->paintingStatus!=40)
            layer->paintingStatus=40;
        else layer->paintingStatus=0;
    });

    connect(leftDockWindow->missionButton->routeConnect,&QPushButton::clicked,[=](){
        if(!layer->helicopter->showRoute){
            layer->helicopter->showRoute=true;
        } else layer->helicopter->showRoute=false;
        layer->update();
    });
    connect(leftDockWindow->detectionButton->detectionbtn,&QPushButton::clicked,[=](){
        layer->helicopter->on_pushButton_find_clicked();
        //layer->generateResponse();
        //layer->initResponseVector();

    });
    connect(layer->helicopter,&Helicopter::initResponseDrone,[=](){
        if(layer->helicopter->droneNumber!=0){
            layer->helicopter->responseMap[layer->helicopter->droneNumber]->setParent(layer);
            layer->helicopter->responseMap[layer->helicopter->droneNumber]->widget->setParent(layer);
            layer->helicopter->responseMap[layer->helicopter->droneNumber]->location=QPoint(layer->helicopter->responseMap[layer->helicopter->droneNumber]->x,layer->helicopter->responseMap[layer->helicopter->droneNumber]->y);
            qDebug()<<layer->helicopter->responseMap[layer->helicopter->droneNumber]->x;
            layer->helicopter->responseMap[layer->helicopter->droneNumber]->temp=QPoint(0,0);
            layer->elementVector.append((Sprite*) layer->helicopter->responseMap[layer->helicopter->droneNumber]);
            layer->helicopter->responseMap[layer->helicopter->droneNumber]->isMarked=true;
            layer->update();
        }
    });

    connect(leftDockWindow->detectionButton->controlDrone,&QPushButton::clicked,layer->helicopter,&Helicopter::on_pushButton_s_clicked);

    leftDockWindow->detectionButton->layout3->addWidget(layer->helicopter->lineEdit);
    leftDockWindow->detectionButton->layout3->addWidget(layer->helicopter->textEdit);

    QThread* animationThread = new QThread();
    connect(leftDockWindow->missionButton->planeMoveBtn,&QPushButton::clicked,[=](){
        Helicopter* hlc = layer->helicopter;
        QSequentialAnimationGroup* group = new QSequentialAnimationGroup(this);
        // 设置动画持续时间
        // 设置动画的起始值和结束值
        for (PlaneMarker* marker : hlc->markerVector) {
            QPropertyAnimation* animation = new QPropertyAnimation(hlc, "location");
            animation->setDuration(2000);
            animation->setEndValue(marker->pos());
            group->addAnimation(animation);
        }
        group->moveToThread(animationThread);
        connect(animationThread, &QThread::started, [=]{
            group->start();
        } );
        animationThread->start();
    });
}


MyMainWindow::~MyMainWindow()
{
}

void MyMainWindow::init()
{
    //----------initalize the mainwindow
    this->setMinimumSize(1200,800);
    this->setMouseTracking(true);
    //----------map load
    this->setWindowTitle("HTML Viewer");
    webView = new QWebView(this);
    QUrl fileUrl = QUrl::fromLocalFile("/home/kirin/桌面/hello/image/map.html");
    webView->load(fileUrl);
    webView->setStyleSheet("QScrollBar:vertical { width: 0; } QScrollBar:horizontal { height: 0; }");
    this->setCentralWidget(webView);
    webView->setMouseTracking(true);

}


void MyMainWindow::resizeEvent(QResizeEvent *event)
{
    // 获取新的窗口大小
    QSize newSize = event->size();
    this->webView->setFixedSize(event->size());
    layer->setFixedSize(event->size());

    rightDockWindowInitX=newSize.width()-stackMap["rightDockWindow"]->width();
    // 处理其他控件的伸缩逻辑，例如更新文本、调整大小等
    if(stackMap.find("rightDockWindow")!=stackMap.end()){
        stackMap["rightDockWindow"]->_dis_x=newSize.width()-25;
        stackMap["rightDockWindow"]->_dis_y=100;
        stackMap["rightDockWindow"]->updateDisplay();
    }
    if(stackMap.find("topRightDockWindow")!=stackMap.end()){
        stackMap["topRightDockWindow"]->_dis_x=newSize.width()-110;
        stackMap["topRightDockWindow"]->updateDisplay();
    }
    if(stackMap.find("bottomDockWindow")!=stackMap.end()){
        stackMap["bottomDockWindow"]->widget->setFixedSize(newSize.width(),100);
        stackMap["bottomDockWindow"]->setFixedSize(newSize.width()+50,150);
        stackMap["bottomDockWindow"]->_dis_y=newSize.height()-150;
        stackMap["bottomDockWindow"]->updateDisplay();
        bottomDockWindow->bottomSideButton->move((event->size().width()-bottomDockWindow->bottomSideButtonWidth)/2,bottomDockWindow->widget->height());
    }
    // 在窗口大小变化时触发的事件
    QMainWindow::resizeEvent(event);

}
void MyMainWindow::setPowerKeyStyle(QPushButton *pushButton){
    pushButton->setStyleSheet("QPushButton{"
                              "border-image:url(:/image/anniu.svg);"//背景色（也可以设置图片）
                              "border-style:outset;"                  //边框样式（inset/outset）
                              "border-width:4px;"                     //边框宽度像素
                              "border-radius:10px;"                   //边框圆角半径像素
                              "border-color:rgba(255,255,255,30);"    //边框颜色
                              "font:bold 24px;"                       //字体，字体大小
                              "color:rgba(0,0,0,100);"                //字体颜色
                              "padding:6px;"                          //填衬
                              "}"
                              "QPushButton:pressed{"
                              "background-color:rgba(100,255,100,200);"
                              "border-color:rgba(255,255,255,30);"
                              "border-style:inset;"
                              "color:rgba(0,0,0,100);"
                              "}"
                              "QPushButton:hover{"
                              "border-image:url(:/image/anniuyixuan.svg);"
                              "border-color:rgba(255,255,255,200);"
                              "color:rgba(0,0,0,200);"
                              "}"
                              );

}
void MyMainWindow::creatQuitMessageBox(){
    QMessageBox *messageBox=new QMessageBox;
    messageBox->setWindowTitle("Tip");
    messageBox->setText("Do you want to quit?");
    QPushButton *yesButton=messageBox->addButton(QMessageBox::Yes);
    QPushButton *noButton=messageBox->addButton(QMessageBox::No);
    setPowerKeyStyle(yesButton);
    setPowerKeyStyle(noButton);

    messageBox->button(QMessageBox::Yes)->setFixedSize(150,50);
    messageBox->button(QMessageBox::No)->setFixedSize(150,50);

    messageBox->setStyleSheet("QLabel{"
                              "color:#4e72b8;"
                              "font-size:24px;"
                              "font-weight:bold;"
                              "padding-bottom:30px;"
                              "padding-top:30px;"
                              "}"
                              "text-align:center;"
                              );


    messageBox->exec();
    if(messageBox->clickedButton()==yesButton){
        this->close();
    }
    if(messageBox->clickedButton()==noButton){
        messageBox->close();
    }
}


